﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "userlistwidget.h"
#include "chatwindow.h"
#include <QLabel>
#include "friendrequestlist.h"
#include "grouprequestlist.h"
#include "avatarselector.h"


class QLabel;
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void closeEvent(QCloseEvent *event) override;
    // 登录成功以后的初始化，用于更新当前用户的显示状态
    void init();

    // get chat window
    std::shared_ptr<ChatWindow> getUserWindow(QString account);
    // get group window
    std::shared_ptr<ChatWindow> getGroupWindow(uint64_t groupID);




signals:
    void closeProgram();

public slots:
    void newChatWindow(QString account, bool show = true, bool isUser = true);

    void addMessageFromUser(int avatar, QString account, QString message, QString time, int messageType);

    void addMessageFromSelf(QString targetAccount, QString message, QString time, int messageType);

    void addmessageFromGroup(int avatar, QString account, uint64_t groupID, QString message, QString time, int messageType);

    void initUserLoginMessage();

    void do_updateFileTransferInfo(QString info);

    void do_addFileInfoToFileList(QString fileInfo);

    void do_updateStatusbarText(QString message);

    void do_userListAddItem(QString account);

    void do_userRequestListAddItem(QString account);

    void do_eraseFriend(QString account);

    void do_updateGroupList(QString groupName);

    void do_updateAvatar(int avatar);

    void do_updateStatusbarPermenantText(QString message, bool connected);

private slots:
    void on_btnUploadFile_clicked();

    void on_btnDownloadFile_clicked();

    void on_btnRefreshFileList_clicked();

    void on_btnAddFriend_clicked();


    void on_btnFriendRequest_clicked();

    void on_btnRefreshUserList_clicked();

    void on_btnCreateGroup_clicked();

    void on_btnRefreshGroupList_clicked();

    void on_btnJoinGroup_clicked();

    void on_listWidgetGroup_itemDoubleClicked(QListWidgetItem *item);

    void on_btnQuitGroup_clicked();

    void on_btnGroupManager_clicked();

    void on_btnUpdateAvatar_clicked();

    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
    UserListWidget * userList;

    QLabel* statusBarText;
    QPushButton* statusBarPermanentText;
    FriendRequestList* friendRequestList;

    GroupRequestList* groupRequestList;
    AvatarSelector* avatarSelector;

    std::map<QString, std::shared_ptr<ChatWindow>> userChatWindows;
    std::map<uint64_t, std::shared_ptr<ChatWindow>> groupChatWindows;

};
#endif // MAINWINDOW_H
